<!--
 * @Description: 1分钱商品专区商品批量添加
 * @Author: Li Yujie
 * @Date: 2020/12/22
 * @Last Modified by: Li Yujie
 * @Last Modified time: 2020/12/22
-->
<template>
    <div class="wrapper">
        <div class="page">
            <!-- header -->
            <div class="header">
                <div class="header-title">
                    批量添加商品
                </div>
            </div>
            <!-- /header -->
            <!-- table -->
            <div class="table-wrapper">
                <el-form
                    :model="ruleForm"
                    :rules="rules"
                    style="height: 100%"
                    ref="ruleForm"
                >
                    <el-table
                        :data="ruleForm.tableData"
                        height="100%"
                        style="width: 100%;"
                        ref="tempLateName"
                        class="data-table"
                        @select-all="handleSelectAll"
                    >
                        <el-table-column
                            align="center"
                            type="selection"
                            width="50"
                            fixed="left"
                        >
                            <template slot-scope="scope">
                                <div
                                    v-for="item in scope.row.product_list"
                                    :key="item.product_no"
                                    class="table-item"
                                >
                                    <el-checkbox
                                        v-model="item.check_status"
                                        :true-label="1"
                                        :false-label="0"
                                    />
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column
                            label="商品信息"
                            width="280"
                            fixed="left"
                        >
                            <template slot-scope="scope">
                                <div class="goods-info">
                                    <img
                                        :src="scope.row.goods_cover"
                                        class="goods-cover"
                                        @click="fnShowPreview(scope.row.goods_cover)"
                                    >
                                    <div class="info">
                                        <div class="goods-info__title">
                                            {{ scope.row.goods_title }}
                                        </div>
                                        <div class="goods-info__no">
                                            商品编号:{{ scope.row.goods_no }}
                                        </div>
                                    </div>
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column
                            label="商品标题"
                            show-overflow-tooltip
                            width="140"
                        >
                            <template slot-scope="scope">
                                <div class="table-item">
                                    <el-form-item
                                        :prop="`tableData.${scope.$index}.goods_title_free`"
                                        :rules="rules.goods_title_free"
                                        style="margin-top: -10px"
                                    >
                                        <el-input
                                            v-model="scope.row.goods_title_free"
                                            size="small"
                                        />
                                    </el-form-item>
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column
                            label="每月限领数量"
                            show-overflow-tooltip
                            align="center"
                            width="110"
                        >
                            <template slot-scope="scope">
                                <div class="table-item">
                                    <el-form-item
                                        :prop="`tableData.${scope.$index}.goods_buy_limit_free`"
                                        :rules="rules.count"
                                        style="margin-top: -10px"
                                    >
                                        <el-input
                                            v-model="scope.row.goods_buy_limit_free"
                                            clearable
                                            size="small"
                                        />
                                    </el-form-item>
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column
                            label="商品规格"
                            width="240"
                            show-overflow-tooltip
                        >
                            <template slot-scope="scope">
                                <div
                                    class="table-item"
                                    v-for="(item, index) in scope.row.product_list"
                                    :key="item.product_no"
                                    :style="`color:${item.check_status ? '#1890FF' : ''};cursor: pointer`"
                                    @click="handleChecked(scope.row, scope.$index, index)"
                                >
                                    <span
                                        v-for="(option,i) in item.spec_list"
                                        :key="i"
                                        style="margin: 0 6px"
                                    >
                                        {{ `${option.spec_name} : ${option.spec_value}` }}
                                    </span>
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column
                            prop=""
                            label="原售价"
                            show-overflow-tooltip
                            align="center"
                        >
                            <template slot-scope="scope">
                                <div
                                    class="table-item"
                                    v-for="item in scope.row.product_list"
                                    :key="item.product_no"
                                >
                                    ￥{{ item.product_price }}
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column
                            label="活动库存"
                            width="270"
                        >
                            <template slot-scope="scope">
                                <div
                                    class="table-item"
                                    v-for="(item,index) in scope.row.product_list"
                                    :key="item.product_no"
                                >
                                    <div
                                        style="display: flex; align-items: center;"
                                    >
                                        <el-select
                                            v-model="item.product_stock_type"
                                            style="width: 126px;margin-bottom: 22px;margin-right: 10px"
                                            size="small"
                                            :disabled="!scope.row.product_list[index].check_status"
                                            @change="handleStockTypeChange($event, scope.$index, index)"
                                            @click.native="clickDisableInput(scope.row, scope.$index, index)"
                                        >
                                            <el-option
                                                label="同步商家库存"
                                                :value="1"
                                            />
                                            <el-option
                                                label="自定义"
                                                :value="2"
                                            />
                                        </el-select>
                                        <el-form-item
                                            :prop="`tableData.${scope.$index}.product_list[${index}]`"
                                            :rules="rules.product_stock_free"
                                            v-if="item.product_stock_type === 2"
                                        >
                                            <el-input
                                                oninput="value = value.replace(/[^\d]/g,'')"
                                                placeholder="活动库存"
                                                v-model.number="item.product_stock_free"
                                                style="width: 84px;"
                                                size="small"
                                                :disabled="!scope.row.product_list[index].check_status"
                                                @click.native="clickDisableInput(scope.row, scope.$index, index)"
                                            />
                                        </el-form-item>
                                    </div>
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column
                            label="商家设置库存"
                            align="center"
                            width="110"
                        >
                            <template slot-scope="scope">
                                <div
                                    class="table-item"
                                    v-for="item in scope.row.product_list"
                                    :key="item.product_no"
                                >
                                    {{ item.product_stock }}
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column
                            label="是否平台补贴"
                            align="center"
                            width="110"
                        >
                            <template slot-scope="scope">
                                <div
                                    class="table-item"
                                    v-for="(item, index) in scope.row.product_list"
                                    :key="item.product_no"
                                >
                                    <div
                                        style="display: flex; align-items: center;justify-content: center"
                                    >
                                        <el-form-item style="margin-top: -4px">
                                            <el-select
                                                v-model="item.shop_subsidy_enable"
                                                size="small"
                                                style="width: 100px"
                                                @change="handleSubsidyChange($event, scope.$index, index)"
                                                :disabled="!scope.row.product_list[index].check_status"
                                                @click.native="clickDisableInput(scope.row, scope.$index, index)"
                                            >
                                                <el-option
                                                    label="平台补贴"
                                                    :value="1"
                                                />
                                                <el-option
                                                    label="商家全部补贴"
                                                    :value="0"
                                                />
                                                <el-option
                                                    label="商家部分补贴"
                                                    :value="2"
                                                />
                                            </el-select>
                                        </el-form-item>
                                    </div>
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column
                            label="商家补贴金额"
                            align="center"
                            width="130"
                        >
                            <template slot-scope="scope">
                                <div
                                    class="table-item"
                                    v-for="(item,index) in scope.row.product_list"
                                    :key="item.product_no"
                                >
                                    <div
                                        style="display: flex; align-items: center;justify-content: center"
                                    >
                                        <el-form-item
                                            :prop="`tableData.${scope.$index}.product_list[${index}]`"
                                            :rules="rules.shop_subsidy_money_by_shop"
                                            style="margin-top: -4px"
                                        >
                                            <el-input
                                                v-model="item.shop_subsidy_money_by_shop"
                                                placeholder="商家补贴金额"
                                                size="small"
                                                clearable
                                                style="width: 120px"
                                                :disabled="!scope.row.product_list[index].check_status || item.shop_subsidy_enable !== 2"
                                                @click.native="clickDisableInput(scope.row, scope.$index, index)"
                                            />
                                        </el-form-item>
                                    </div>
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column
                            prop=""
                            label="排序号"
                            align="center"
                            width="120"
                        >
                            <template slot-scope="scope">
                                <div class="table-item">
                                    <el-form-item
                                        :prop="`tableData.${scope.$index}.goods_sort_free`"
                                        :rules="rules.goods_sort_free"
                                        style="margin-top: -8px"
                                    >
                                        <el-input
                                            oninput="value = value.replace(/[^\d]/g,'')"
                                            v-model.number="scope.row.goods_sort_free"
                                            clearable
                                            size="small"
                                        />
                                    </el-form-item>
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column
                            prop=""
                            label="活动状态设置"
                            align="center"
                            width="200px"
                        >
                            <template slot-scope="scope">
                                <el-switch
                                    v-model="scope.row.goods_status_free"
                                    active-text="上架"
                                    inactive-text="下架"
                                    :active-value="1"
                                    :inactive-value="0"
                                />
                            </template>
                        </el-table-column>
                    </el-table>
                </el-form>
            </div>
            <!-- /table -->
            <div class="btn-group">
                <el-button
                    size="medium"
                    style="width: 120px"
                    @click="handleCancel()"
                >取消
                </el-button>
                <el-button
                    size="medium"
                    type="primary"
                    style="width: 120px"
                    @click="handleSubmit()"
                >完成
                </el-button>
            </div>
        </div>

        <!-- 图片预览 -->
        <img-preview ref="preview"/>

        <!-- 预算提示弹窗 -->
        <show-dialog
            ref="showDialog"
            :dialog="showDialog"
            :width="360"
            :cancel-text="'取消'"
            :confirm-text="'确定'"
            :title="'温馨提示'"
            :content="`当前新人专区的平台补贴支出在计划时间${ '2021/5/24-2021/6/24' }已无预算，暂无法添加新人专区商品`"
            @close="showDialog = false"
            @emit="() => {
                this.$router.push({
                    path: '/student/zero-area-list'
                });
            }"
        />
    </div>
</template>

<script>
/**
 * 页面接收参数
 * @param goodsList - 所选商品列表
 */
import showDialog from '@/components/common/dialog.vue';
import {back} from '@/mixin/back';
import imgPreview from '@/components/common/img-preview';

export default {
    name: "ZeroGoodsEditBatch",
    data() {
        const validateStock = (rule, value, callback) => {

            if (value.check_status && !value.product_stock_free && value.product_stock_free !== 0) {
                callback(new Error('请输入活动库存'));
            } else {
                callback();
            }
        };

        return {
            ruleForm: {
                tableData: [],
            },
            rules: {
                goods_title_free: [
                    {required: true, message: '请输入商品标题', trigger: 'blur'}
                ],
                product_stock_free: [
                    {validator: validateStock, trigger: 'blur'},
                ],
                goods_sort_free: [
                    {required: true, message: '请输入排序号', trigger: 'blur'}
                ],
                goods_buy_limit_free: [
                    {required: true, message: '请输入每月限领数量', trigger: 'blur'}
                ]
            },
            showDialog: false
        };
    },
    mixins: [back],
    components: {
        imgPreview,
        showDialog
    },
    mounted() {
        this.$store.commit('SET_HEADER', [{title: "APP后台"}, {title: '商城'}, {
            title: '1分钱商品专区',
            path: '/student/zero-area-list'
        }, {title: '批量添加商品'}]);
        this.init();
    },

    methods: {

        init() {
            let goodsList = JSON.parse(this.$route.query.goodsList);

            goodsList.map(item => {
                item.goods_title_free = item.goods_title;
                item.goods_sort_free = 99;
                item.goods_buy_limit_free = 0;
                item.product_list.map(subItem => {
                    subItem.product_stock_type = 1;
                    subItem.product_stock_free = -1;
                    subItem.check_status = 0;
                    subItem.shop_subsidy_enable = 1;
                    subItem.shop_subsidy_money_by_shop = 0;
                });
            });
            this.ruleForm.tableData = goodsList;
        },
        /**
         * 查看图片
         * @param   {String}    images      //单图预览
         * @param   {Array}     images      //多图预览
         */
        fnShowPreview(images) {
            if (images && Object.prototype.toString.call(images) == '[object String]') {
                this.$refs.preview.show([
                    images
                ]);
            } else if (images && Object.prototype.toString.call(images) == '[object Array]') {
                this.$refs.preview.show(images);
            } else {
                this.$message({
                    type: 'warning',
                    message: '图片类型错误'
                });
            }
        },
        /**
         * 取消返回列表
         */
        handleCancel() {
            this.$confirm('返回后将不会保存您所修改的内容,是否返回?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$router.push({
                    path: '/student/zero-area-list'
                });
            });
        },

        /**
         * 提交数据
         */
        handleSubmit() {

            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    console.log(this.ruleForm.tableData);

                    let tableData = [...this.ruleForm.tableData];
                    let params = {
                        goods_json: JSON.stringify(tableData)
                    };

                    this.$post('/student/shop_vip_free%5Cadd_batch', params, resp => {
                        if (resp.code === 1) {
                            if(resp.data.result === 1) {
                                this.$notify({
                                    title: '提示',
                                    message: '保存成功',
                                    type: 'success',
                                    duration: 2000
                                });
                                setTimeout(() => {
                                    this.$router.push({
                                        path: '/student/zero-area-list'
                                    });
                                }, 500);
                            } else {
                                this.$zdConfirm(`当前一卡通1分钱专区的平台补贴支出${resp.data.result === -1 ? '不在财控计划时间内' : '在计划时间已无预算'} ，暂无法添加一卡通1分钱商品，如需要增加预算，请联系平台财务人员。`, '温馨提示', {
                                    confirmButtonText: '知道了',
                                    hasCancelButton: false,
                                    type: 'warning'
                                }).then(() => {});
                            }
                        } else {
                            this.$notify({
                                title: '提示',
                                message: resp.msg,
                                duration: 2000,
                                type: 'warning'
                            });
                        }
                    });
                }
            });
        },

        /**
         * 点击规格选中当前规格
         * @param row
         * @param rowIndex
         * @param index
         */
        handleChecked(row, rowIndex, index) {

            if (row.product_list[index].check_status) {
                row.product_list[index].check_status = 0;
            } else {
                row.product_list[index].check_status = 1;
            }
        },

        /**
         * 点击禁用的input
         * @param row
         * @param rowIndex
         * @param index
         */
        clickDisableInput(row, rowIndex, index) {
            if (!row.product_list[index].check_status) {
                this.$message.warning('请勾选该规格');
            }
        },

        /**
         * 点击全选
         * @param val
         */
        handleSelectAll(val) {
            this.ruleForm.tableData.map(item => {
                item.product_list.map(subItem => {
                    if (val.length) {
                        subItem.check_status = 1;
                    } else {
                        subItem.check_status = 0;
                    }
                });
            });
        },

        /**
         * 选择库存类型
         * @param val
         * @param tableIndex
         * @param skuIndex
         */
        handleStockTypeChange(val, tableIndex, skuIndex) {
            if(val === 1) {
                this.ruleForm.tableData[tableIndex].product_list[skuIndex].product_stock_free = -1;
            } else {
                this.ruleForm.tableData[tableIndex].product_list[skuIndex].product_stock_free = 0;
            }
        },

        /**
         * 是否平台补贴
         * @param val
         * @param tableIndex
         * @param skuIndex
         */
        handleSubsidyChange(val, tableIndex, skuIndex) {
            if(val === 1) {
                this.ruleForm.tableData[tableIndex].product_list[skuIndex].shop_subsidy_money_by_shop = 0;
            }
        }
    }
};
</script>

<style scoped lang="less">

.wrapper {
    width: 100%;
    position: relative;
    box-sizing: border-box;

    .page {
        display: flex;
        height: calc(100% - 24px);
        flex-direction: column;
        position: relative;
        margin: 12px;
        background: #fff;
        box-sizing: border-box;
        /*header*/

        .header {
            height: 68px;
            padding: 24px;
            box-sizing: border-box;
            box-shadow: 0px -1px 0px 0px #F0F2F5;
            display: flex;
            align-items: center;
            justify-content: space-between;
            border-bottom: 1px solid #F0F2F5;

            .header-title {
                color: #333333;
                font-size: 20px;
                height: 20px;
                font-weight: 500;
                position: relative;
                margin: 24px 0 24px 12px;

                &::before {
                    content: '';
                    position: absolute;
                    left: -8px;
                    top: -1px;
                    width: 3px;
                    height: 20px;
                    background: #1890FF;
                }
            }
        }

        .table-wrapper {
            margin-top: 24px;
            padding: 0 24px;
            flex: 1;
            height: 0;
            .goods-info {
                display: flex;
                align-items: center;

                .goods-cover {
                    width: 80px;
                    height: 80px;
                    cursor: pointer;
                    margin-right: 12px;
                    flex-shrink: 0;
                    object-fit: cover;
                }

                .info {
                    display: flex;
                    flex-direction: column;
                    justify-content: space-between;
                    font-size: 12px;
                    font-family: PingFangSC-Regular, PingFang SC;
                    font-weight: 400;
                    color: #333333;

                    .goods-info__title {
                        font-size: 14px;
                        display: -webkit-box;
                        -webkit-box-orient: vertical;
                        overflow: hidden;
                        -webkit-line-clamp: 2;
                        font-family: PingFangSC-Regular, PingFang SC;
                        font-weight: 400;
                        color: #333333;
                        margin-bottom: 12px;
                    }
                }
            }
        }

        .btn-group {
            height: 72px;
            display: flex;
            align-items: center;
            justify-content: center;

            .el-button {
                margin: 0 24px;
            }
        }

        .table-item {
            margin: 28px 0;
            height: 30px;
        }
    }
    .data-table {
        ::-webkit-scrollbar {
            width: 6px;
            height: 6px;
        }
    }
    /deep/ .el-table {
        th {
            background: #f2f2f2 !important;
        }

        td {
            background: #fff !important;
        }
    }
}
</style>
